<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta name="viewport"
	      content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>事件委托</title>
</head>
<body>
<ul id="items">
	<li>AAAA</li>
	<li>BBBB</li>
	<li>CCCC</li>
	<li>DDDD</li>
	<div>EEEE</div>
</ul>
</body>
</html>
<script>
	function addEventListener(el,type,fn,selector) {
		if (typeof el ==='string'){
			el=document.querySelector(el);
		}
		if (!selector){
			el.addEventListener(type,fn);
		}else {
			el.addEventListener(type,function (e) {
				const target=e.target;
				if (target.matches(selector)){
					fn.call(target,e)
				}
			})
		}
	}

	addEventListener('#items','click',function () {
		console.log(this.innerHTML)
	},'li')
</script>
